#!/bin/bash
#
# Preserve random seed across reboot. See urandom(4).
#

load() {
  if seed="$(/usr/bin/batocera-settings-get -f /boot/batocera-boot.conf randomseed)"
  then
    printf "Restoring random seed: "
    echo $seed | xxd -r -ps > /dev/urandom
    echo "done."
  fi
}

save() {
  printf "Saving random seed: "
  mount -o remount,rw /boot
  /usr/bin/batocera-settings-set -f /boot/batocera-boot.conf randomseed `xxd -ps -l 512 -c 512 /dev/urandom`
  mount -o remount,ro /boot
  echo "done."
}

case "$1" in
  start)
    load
    save
    ;;
  stop)
    save
    ;;
  *)
    echo "usage: $0 {start|stop}"
    ;;
esac

exit 0
